package cn.yuhh.servlet;

import cn.yuhh.entity.FoodType;
import cn.yuhh.factory.BeanFactory;
import cn.yuhh.service.IFoodTypeService;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by Brill on 2016/12/10.
 */
@WebServlet(name = "FoodTypeServlet", urlPatterns = {"/foodType"})

public class FoodTypeServlet extends HttpServlet {

    private IFoodTypeService service = BeanFactory.getInstance("foodTypeService", IFoodTypeService.class);

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("type/html;charset=utf-8");

        String method = request.getParameter("method");

        if ("add".equals(method)) {
            add(request, response);
        } else if ("list".equals(method)) {
            list(request, response);
        } else if ("delete".equals(method)) {
            delete(request, response);
        } else if ("show".equals(method)) {
            show(request, response);
        } else if ("update".equals(method)) {
            update(request, response);
        }

        list(request, response);
    }

    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<FoodType> types = service.query();

        request.setAttribute("types", types);
        request.getRequestDispatcher("sys/foodtype/cuisineList.jsp").forward(request, response);

    }

    public void add(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {
        String name = request.getParameter("name");

        if (name != null) {
            FoodType foodType = new FoodType();
            foodType.setTypeName(name);

            service.add(foodType);
            list(request, response);
        }
    }

    public void delete(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {
        int id = Integer.parseInt(request.getParameter("id"));

        service.delete(id);
        list(request, response);
    }

    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        System.out.println(id);

        FoodType type = service.findById(id);
        String typeName = request.getParameter("typeName");
        type.setTypeName(typeName);
        service.update(type);
        list(request, response);
    }

    public void show(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {
        int id = Integer.parseInt(request.getParameter("id"));

        FoodType type = service.findById(id);
        request.setAttribute("type", type);
        request.getRequestDispatcher("/sys/foodtype/updateCuisine.jsp").forward(request, response);
        System.out.println(id);
    }
}
